package com.yinet.wsyy.service.impl;

import com.google.gson.Gson;
import com.yinet.common.utils.http.HttpUtils;
import com.yinet.system.domain.Holiday;
import com.yinet.system.mapper.HolidayMapper;
import com.yinet.wsyy.domain.WsyySetting;
import com.yinet.wsyy.mapper.WsyySettingMapper;
import com.yinet.wsyy.service.IWsyySettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 网上预约配置Service业务层处理
 *
 * @author yinet
 * @date 2019-09-02
 */
@Service
public class WsyySettingServiceImpl implements IWsyySettingService {
    @Autowired
    private WsyySettingMapper wsyySettingMapper;
    @Autowired
    private HolidayMapper holidayMapper;

    /**
     * 查询网上预约配置
     *
     * @return 网上预约配置
     */
    @Override
    public WsyySetting selectWsyySetting() {
        return wsyySettingMapper.selectWsyySetting();
    }


    /**
     * 修改网上预约配置
     *
     * @param wsyySetting 网上预约配置
     * @return 结果
     */
    @Override
    public int updateWsyySetting(WsyySetting wsyySetting) {
        return wsyySettingMapper.updateWsyySetting(wsyySetting);
    }

    @Override
    public int updateWsyyHoliday() {
        List<String> nowYearAllDay = getNowYearAllDay();
        List<Holiday> holidays = new ArrayList<>();
        for (String date : nowYearAllDay) {
            String s = HttpUtils.sendGet("http://timor.tech/api/holiday/info/"+date,"");
            Map map = new Gson().fromJson(s, Map.class);
            Holiday holiday = new Holiday(date, Integer.parseInt(map.get("data").toString().substring(0, 1)));
            holidays.add(holiday);
        }
        holidayMapper.truncateHoliday();
        for (Holiday ho : holidays) {
            holidayMapper.insertHoliday(ho);
        }
        return 0;
    }

    public static List<String> getNowYearAllDay() {
        List<String> resultList = new ArrayList<String>();
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int i = aCalendar.get(Calendar.YEAR);
        for (int j = 1; j < 13; j++) {
            resultList.addAll(getAllMonthDays(i + "-" + j));
        }
        return resultList;
    }

    /**
     * 获取某一月份的所有日期
     * date 年月;格式:"2018-10"
     *
     * @return
     */
    public static List<String> getAllMonthDays(String date) {
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5));
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int days = a.get(Calendar.DATE);
        List<String> resultList = new ArrayList<String>();
        for (int i = 1; i <= days; i++) {
            String day = String.valueOf(year) + "-" + fillZero(month) + "-" + fillZero(i);
            resultList.add(day);
        }
        return resultList;
    }

    public static String fillZero(int num) {
        if (num < 10) {
            return "0" + num;
        } else {
            return String.valueOf(num);
        }
    }
}
